﻿using System;

public class Node<T>
{
    public Node<T> Next { get; set; }
    public T Data { get; set; }  // 修正大小写 

    public Node(T t)
    {
        Next = null;
        Data = t;
    }
}

public class GenericList<T>  // 修正calss为class 
{
    private Node<T> head;
    private Node<T> tail;

    public GenericList()  // 修正构造函数名称 
    {
        tail = head = null;
    }

    public Node<T> Head => head;

    public void Add(T t)
    {
        Node<T> n = new Node<T>(t);
        if (tail == null)
        {
            head = tail = n;
        }
        else
        {
            tail.Next = n;
            tail = n;
        }
    }

    // 新增ForEach方法 
    public void ForEach(Action<T> action)
    {
        Node<T> current = head;
        while (current != null)
        {
            action(current.Data);
            current = current.Next;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // 整型链表操作 
        GenericList<int> intList = new GenericList<int>();
        for (int x = 0; x < 10; x++)
        {
            intList.Add(x);
        }

        // 打印元素 
        Console.WriteLine("链表元素：");
        intList.ForEach(x => Console.Write(x + " "));

        // 求极值与求和 
        int max = int.MinValue;
        int min = int.MaxValue;
        int sum = 0;
        //lambda表达式
        intList.ForEach(x => {
            max = Math.Max(max, x);
            min = Math.Min(min, x);
            sum += x;
        });

        Console.WriteLine($"\n最大值：{max}  最小值：{min}  总和：{sum}");

        // 字符串链表操作 
        GenericList<string> strList = new GenericList<string>();
        for (int x = 0; x < 10; x++)
        {
            strList.Add("str" + x);
        }

        Console.WriteLine("\n字符串链表：");
        strList.ForEach(s => Console.Write(s + " "));

        Console.ReadKey();
    }
    
}